<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../css/default.css" />
<link rel="stylesheet" type="text/css" href="../css/css.css" />
<link rel="stylesheet" type="text/css"
	href="../jquery/redmond/jquery-ui-1.9.0.custom.min.css" />
<script type="text/javascript" src="../jquery/jquery-1.8.2.min.js"></script>
<script type="text/javascript"
	src="../jquery/jquery-ui-1.9.0.custom.min.js"></script>
<script type="text/javascript" src="../jquery/jquery-extends.js"></script>
<script>
	var oldUsername = null;

	$(function() {
		var username = $("#username"), fullname = $("#fullname"), roles = $("#roles"),userId=$("#userId"), allFields = $(
				[]).add(username).add(fullname).add(roles).add(userId), tips = $(".validateTips");

		function updateTips(t) {
			tips.text(t).addClass("ui-state-highlight");
			setTimeout(function() {
				tips.removeClass("ui-state-highlight", 1500);
			}, 500);
		}

		function checkLength(o, n) {
			if (o.val().length < 1) {
				o.addClass("ui-state-error");
				updateTips(n + "不允许为空.");
				return false;
			} else {
				return true;
			}
		}

		function loadUserInfos() {
			$.ajax({
				url : "../../user/list.htm",
				type : "POST",
				cache : false,
				dataType : "json",
				success : function(data, textStatus, jqXHR) {
					$("#userinfos").find("tr:gt(0)").remove();
					if (data != null && data != "") {
						$.each(data.rows,function(key, value) {
								var html = "<tr><td class='username'>"
													+ value.username
													+ "</td><td class='fullname'>"
													+ value.fullName
													+ "</td><td><input type='hidden' name='role' value='"
								+ value.role + 
								"' /><input type='hidden' name='userId' value='"
								+ value.userId + 
								"' /><button class=\"btnUpdate\">修改</button>"
													+ "<button class=\"btnDelete\">删除</button>"
													+ "<button class=\"btnReset\">重置密码</button></td></tr>";
											$("#userinfos")
													.append(html);
										});

						$('table tr:even').addClass('hover'); 
					}
				}
			});
		} 

		$("#userInfoFormWin").dialog({
			autoOpen : false,
			height : 400,
			width : 370,
			modal : true,
			buttons : {
				"确定" : function() {
					var me = this;
					var bValid = true;
					allFields.removeClass("ui-state-error");

					bValid = bValid && checkLength(username, "登录名");
					bValid = bValid && checkLength(fullname, "姓名");

					var sels = [];
					$('select option:selected').each(function() {
						sels.push($(this).val());
					});

					if (sels.length > 0) {
						bValid = bValid && true;
					} else {
						bValid = bValid && false;
						roles.addClass("ui-state-error");
						updateTips("权限不允许为空.");
					}
					var result = sels.join(',');



					if (bValid) {
						$.ajax({
							url : "../../user/save.htm",
							type : "POST",
							data : {
								username : username.val(),
								fullName : fullname.val(),
								role : result,
								oleUsername : oldUsername
							},
							cache : false,
							dataType : "json",
							success : function(data, textStatus, jqXHR) {
								if (data != null && data != "") {
									if (data.success) {
										$(me).dialog("close");
										loadUserInfos();
									} else {
										updateTips(data.message);
									}
								}
							}
						});
					}
				},
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				allFields.val("").removeClass("ui-state-error");
			}
		});

		$("input.btnAdd").click(function(event) {
			oldUsername = null;
			$("#userInfoFormWin").dialog("open");
		});
		
		$(document).on('click','button.btnUpdate',function() {
			oldUsername = $(this).parents("tr").children(
			"td.username")[0].innerHTML;

			username.val(oldUsername);
			fullname.val($(this).parents("tr").children(
					"td.fullname")[0].innerHTML);
		
			var rs = $(this).parents("td").children(
					"input[type=hidden]")[0].value;
			roles.val(rs.split(','));
			
			userId.val($(this).parents("td").children(
			"input[type=hidden]")[1].value);
		
			$('#userInfoFormWin').dialog("open").dialog('option',
					'title', '修改用户');
		});	
		
		$(document).on('click','button.btnDelete',function() {
			var btn = $(this);
			var uname = $(btn).parents("tr").children("td.username")[0].innerHTML;
			$.confirm('你确定要删除用户 ' + uname + " 吗？","提示",
					function() {
						$.ajax({
								url : "../../user/delete.htm",
								type : "POST",
								data : {
									username : uname
								},
								cache : false,
								dataType : "json",
								success : function(data,textStatus,jqXHR) {
									if (data != null && data != "") {
										if (data.success) {
											$(btn).parents("tr").remove();
											$('table tr').removeClass('hover');
											$('table tr:even').addClass('hover');
										} else {
											$.alert(data.message,"提示");
										}
									}
								}
						});
					});

		});

		$(document).on('click','button.btnReset',function() {
					var uname = $(this).parents("tr").children(
							"td.username")[0].innerHTML;
					$.confirm('确定要重置用户 ' + uname + "的登录密码吗？", "提示", function() {
						$.ajax({
							url : "../../user/resetpwd.htm",
							type : "POST",
							data : {
								username : uname
							},
							cache : false,
							dataType : "json",
							success : function(data, textStatus, jqXHR) {
								if (data != null && data != "") {
									if (data.success) {
										$.alert("重置密码成功", "提示");
									} else {
										$.alert(data.message, "提示");
									}
								}
							}
						});
					});
				});

		loadUserInfos();
	});
</script>
</head>
<body>
    <input type="image" src="../../css/images/AddUser.png" name="imgSubmit" value="添加用户" class="btnAdd" />
	<!--<button class="btnAdd">添加用户</button>-->
	<table id="userinfos" cellpadding="0" cellspacing="0">
		<caption>系统用户信息列表</caption>
		<thead>
			<tr>
				<th>登录名</th>
				<th>姓名</th>
				<th width="240">操作</th>
			</tr>
		</thead>
	</table>

	<div id="userInfoFormWin" title="添加用户">
		<p class="validateTips">所有输入框均不允许为空.</p>
		<form><input type="hidden" name="userId" id="userId" />
			<label for="username">登录名:</label> <input type="text" name="username"
				id="username" class="text ui-widget-content ui-corner-all"
				style="width: 220px;" /><br /> <label for="fullname">姓&nbsp;&nbsp;&nbsp;名:</label>
			<input type="text" name="fullname" id="fullname" value=""
				class="text ui-widget-content ui-corner-all" style="width: 220px;" />
			<br /> <label for="roles">权&nbsp;&nbsp;&nbsp;限:</label> <select
				class="select ui-widget-content ui-corner-all" name="roles"
				id="roles" multiple="multiple" size="7" style="width: 220px;">
				<option value="0">管理员</option>
				<!--<option value="1">入库</option>-->
				<!--<option value="2">出库</option>-->
				<!--<option value="3">退单</option>-->
				<option value="4">查看出入库记录</option>
				<!--<option value="5">查询日志</option>-->
				<!--<option value="6">撤箱</option>-->
			</select>
		</form>
	</div>
</body>
</html>